﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace library
{
    public partial class 密码重置 : Form
    {
        public MySqlConnection conn = new DBclass().getConn();
        public 密码重置()
        {
            InitializeComponent();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            conn.Open();
            this.dataGridView1.Columns["是否重置"].Visible = true;
            string username = textBox3.Text;
            string uname = textBox1.Text;
            string sex = comboBox1.Text;
            string major = comboBox2.Text;
            string identity = comboBox3.Text;
            string sql = "select uID '学号',uname '姓名',sex '性别',college '学院',major '专业班级',identity '身份' from user_info where uID like @uID and uname like @uname and sex like @sex and major like @major and identity like @identity";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.Parameters.Add("@uID", MySqlDbType.VarChar).Value = "%" + username + "%";
            cmd.Parameters.Add("@uname", MySqlDbType.VarChar).Value = "%" + uname + "%";
            cmd.Parameters.Add("@sex", MySqlDbType.VarChar).Value = "%" + sex + "%";
            cmd.Parameters.Add("@major", MySqlDbType.VarChar).Value = "%" + major + "%";
            cmd.Parameters.Add("@identity", MySqlDbType.VarChar).Value = "%" + identity + "%";
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();
        }

        private void 密码重置_Load(object sender, EventArgs e)
        {
            this.dataGridView1.Columns["是否重置"].Visible = false;

            conn.Open();
            string sql = "select distinct major from user_info";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                comboBox2.Items.Add(reader[0]);
            }
            conn.Close();

            conn.Open();
            string sql1 = "select distinct identity from user_info";
            MySqlCommand cmd1 = new MySqlCommand(sql1, conn);
            MySqlDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read())
            {
                comboBox3.Items.Add(reader1[0]);
            }
            conn.Close();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            conn.Open();
            if (dataGridView1.Rows.Count > 0)
            {
                int j = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[0];
                    Boolean flag = Convert.ToBoolean(checkCell.Value);
                    if (flag)
                    {
                        string uID = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        
                        string sql = "update user_info set upwd='123456' where uID=@uID";
                        MySqlCommand cmd = new MySqlCommand(sql, conn);
                        cmd.Parameters.Add("@uID", MySqlDbType.Int32).Value = uID;
                        j += cmd.ExecuteNonQuery();
                        dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                }
                if(j>0)
                {
                    MessageBox.Show("该用户密码已重置");
                }
                conn.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.EndEdit();//停止编辑状态
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[0].Value = true;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.EndEdit();//结束编辑状态
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.Cells[0].Value = false;

                }
            }
        }
    }
}
